﻿using System;
using System.Collections.Generic;
using Engine;
using Game;

namespace RuthlessConquest {
    public class EditLobbyServersDialog : Dialog {
        public const string fName = "RCEditLobbyServersDialog";

        public class EditLobbyServerItemWidget : CanvasWidget {
            public TextBoxWidget TextBox;
            public ButtonWidget RemoveButton;
            public string Address;
            public Action<EditLobbyServerItemWidget> RemoveHandler;
            public bool Valid;

            public EditLobbyServerItemWidget(string address, Action<EditLobbyServerItemWidget> removeHandler) {
                Address = address;
                RemoveHandler = removeHandler;
                Size = new Vector2(480f, 54f);
                HorizontalAlignment = WidgetAlignment.Center;
                Valid = Uri.CheckHostName(address) != UriHostNameType.Unknown;
                CanvasWidget textBoxCanvas = new() {
                    Size = new Vector2(430f, 40f), Margin = new Vector2(0f, 00f), VerticalAlignment = WidgetAlignment.Center
                };
                AddChildren(textBoxCanvas);
                textBoxCanvas.AddChildren(
                    new RectangleWidget {
                        HorizontalAlignment = WidgetAlignment.Stretch,
                        VerticalAlignment = WidgetAlignment.Stretch,
                        FillColor = Color.Transparent,
                        OutlineColor = new Color(64, 64, 64, 64)
                    }
                );
                textBoxCanvas.AddChildren(
                    TextBox = new TextBoxWidget {
                        Text = Address,
                        Color = Valid ? Color.White : Color.Red,
                        Margin = new Vector2(5f, 0f),
                        Size = new Vector2(420f, 40f),
                        MaximumLength = 253
                    }
                );
                Subtexture plus = ContentManager.Get<Subtexture>("Textures/Atlas/Plus");
                BitmapButtonWidget bitmapButtonWidget = new() {
                    Size = new Vector2(40f, 40f), HorizontalAlignment = WidgetAlignment.Far, NormalSubtexture = plus, ClickedSubtexture = plus
                };
                bitmapButtonWidget.m_rectangleWidget.LayoutTransform = Matrix.CreateFromYawPitchRoll(0f, 0f, MathF.PI / 4f);
                RemoveButton = bitmapButtonWidget;
                AddChildren(RemoveButton);
            }

            public override void Update() {
                string text = TextBox.Text;
                if (text != Address) {
                    if (Uri.CheckHostName(text) != UriHostNameType.Unknown) {
                        Valid = true;
                        TextBox.Color = Color.White;
                        Address = text;
                    }
                    else {
                        Valid = false;
                        TextBox.Color = Color.Red;
                    }
                }
                if (RemoveButton.IsClicked) {
                    RemoveHandler?.Invoke(this);
                }
            }
        }

        public ScrollPanelWidget ScrollPanel;
        public StackPanelWidget ServersList;
        public ButtonWidget AddButton;
        public ButtonWidget OkButton;
        public ButtonWidget CancelButton;
        public Action<int> EditedHandler;

        public EditLobbyServersDialog(Action<int> editedHandler) {
            EditedHandler = editedHandler;
            Size = new Vector2(600f, 400f);
            ClampToBounds = true;
            HorizontalAlignment = WidgetAlignment.Center;
            VerticalAlignment = WidgetAlignment.Center;
            AddChildren(new RectangleWidget { FillColor = Color.Black, OutlineColor = new Color(128, 128, 128, 128), OutlineThickness = 2f });
            AddChildren(new InterlaceWidget());
            StackPanelWidget container = new() {
                Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Stretch
            };
            AddChildren(container);
            container.AddChildren(
                new LabelWidget {
                    Text = LanguageControl.Get(fName, "1"),
                    Color = Color.White,
                    HorizontalAlignment = WidgetAlignment.Center,
                    Margin = new Vector2(0f, 8f)
                }
            );
            ScrollPanel = new ScrollPanelWidget {
                Direction = LayoutDirection.Vertical,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Stretch,
                Margin = new Vector2(0f, 3f)
            };
            container.AddChildren(ScrollPanel);
            ServersList = new StackPanelWidget { Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Center };
            ScrollPanel.AddChildren(ServersList);
            StackPanelWidget buttonsPanel = new() {
                Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center, Margin = new Vector2(0f, 5f)
            };
            container.AddChildren(buttonsPanel);
            buttonsPanel.AddChildren(
                AddButton = new BevelledButtonWidget {
                    Text = LanguageControl.Get(fName, "2"), Color = Color.White, Size = new Vector2(160f, 60f), Margin = new Vector2(15f, 0f)
                }
            );
            buttonsPanel.AddChildren(
                OkButton = new BevelledButtonWidget {
                    Text = LanguageControl.Ok, Color = Color.White, Size = new Vector2(160f, 60f), Margin = new Vector2(15f, 0f)
                }
            );
            buttonsPanel.AddChildren(
                CancelButton = new BevelledButtonWidget {
                    Text = LanguageControl.Cancel, Color = Color.White, Size = new Vector2(160f, 60f), Margin = new Vector2(15f, 0f)
                }
            );
            foreach (string lobbyServer in SettingsManager.LobbyServers) {
                ServersList.AddChildren(new EditLobbyServerItemWidget(lobbyServer, item => { ServersList.RemoveChildren(item); }));
            }
            ScrollPanel.ScrollPosition = 0f;
        }

        public override void Update() {
            if (AddButton.IsClicked) {
                ServersList.AddChildren(new EditLobbyServerItemWidget("", item => { ServersList.RemoveChildren(item); }));
                ScrollPanel.ScrollPosition = ScrollPanel.m_scrollAreaLength;
            }
            if (OkButton.IsClicked) {
                List<string> newLobbyServers = new();
                foreach (Widget widget in ServersList.Children) {
                    if (widget is EditLobbyServerItemWidget item
                        && item.Valid
                        && !newLobbyServers.Contains(item.Address)) {
                        newLobbyServers.Add(item.Address);
                    }
                }
                SettingsManager.LobbyServers = newLobbyServers;
                DialogsManager.HideDialog(this);
                EditedHandler?.Invoke(newLobbyServers.Count);
            }
            if (Input.Back
                || CancelButton.IsClicked
                || (Input.Press.HasValue && !HitTest(Input.Press.Value))) {
                DialogsManager.HideDialog(this);
            }
        }
    }
}